import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provide/person.dart';
import './login.dart';

class RegisterPage extends StatefulWidget {
  final Widget child;

  RegisterPage({Key key, this.child}) : super(key: key);

  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  Animation<double> _animation;

  TextEditingController _unameController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  GlobalKey _formKey= new GlobalKey<FormState>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController = new AnimationController(
      vsync: this,duration: Duration(milliseconds: 2000)
    );

    _animation = new Tween(begin: 400.0,end: 0.0).animate(_animationController)
      ..addListener((){
        setState(() {
          
        });
      });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text("注册页面"),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
        child: Form(
          key: _formKey, //设置globalKey，用于后面获取FormState
          autovalidate: true, //开启自动校验
          child: Column(
            children: <Widget>[
              TextFormField(
                  autofocus: true,
                  controller: _unameController,
                  decoration: InputDecoration(
                      labelText: "用户名",
                      hintText: "用户名",
                      icon: Icon(Icons.person)
                  ),
                  // 校验用户名
                  validator: (v) {
                    return v
                        .trim()
                        .length > 0 ? null : "用户名不能为空";
                  }

              ),
              TextFormField(
                  controller: _pwdController,
                  decoration: InputDecoration(
                      labelText: "密码",
                      hintText: "您的注册密码",
                      icon: Icon(Icons.lock)
                  ),
                  obscureText: true,
                  //校验密码
                  validator: (v) {
                    return v
                        .trim()
                        .length > 5 ? null : "密码不能少于6位";
                  }
              ),
              // 登录按钮
              InkWell(
                onTap: (){
                  register(context);
                },
                child: Container(
                  margin: EdgeInsets.only(top: 30),
                  height: ScreenUtil().setHeight(75),
                  width: _animation.value>42?_animation.value:42,
                  decoration:BoxDecoration(
                    borderRadius: BorderRadius.circular(30),
                    color: Theme.of(context).primaryColor,
                  ),
                  alignment: Alignment.center,
                  child: _animation.value > 75? new Text("立即注册",
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                            color: Colors.white
                            )
                          )
                          : CircularProgressIndicator(
                            valueColor: AlwaysStoppedAnimation(Colors.white),
                          )
                  ),
              ),
              SizedBox(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  InkWell(
                    onTap: (){
                      Navigator.push(context, MaterialPageRoute(
                        builder: (context){
                          return RegisterPage();
                        }
                      ));
                    },
                    child: Text(
                    '已有账户？去登录吧',
                      style: TextStyle(
                        color: Theme.of(context).primaryColor,
                      ),
                    ),
                  )
                ],
              )
            ]
          )
        )
      )
    );
  }

  // 点击登陆
  void register(context) async {
    if((_formKey.currentState as FormState).validate()){
      // 验证通过提交数据
      // 开始执行动画
      await _animationController.forward();
      Provide.value<PersonnalProvider>(context).register(_unameController.text,_pwdController.text,context);
      // print('登录后');
      // await GetMyLike();
      await _animationController.reverse();
    }
    
  }
}